package com.t.bricks.bussiness.service.currentprice.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.t.bricks.bussiness.db.dao.currentprice.CurrentPriceDao;
import com.t.bricks.bussiness.db.entity.currentprice.CurrentPrice;
import com.t.bricks.bussiness.model.currentprice.ConditionCurrentPrice;
import com.t.bricks.bussiness.service.currentprice.CurrentPriceService;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.model.OrderInfoList;
import com.t.bricks.model.Page;
import com.t.bricks.utils.function.file.ZipUtil;
import com.t.bricks.utils.spring.ModuleUtil;

/**
 * 当前售价'CurrentPrice表'基本业务操作类接口实现
 */
@Service
public class CurrentPriceServiceImpl implements CurrentPriceService {

	@Autowired
	private CurrentPriceDao currentPriceDao;

	/**
	 * 修改
	 * @param currentPrice 当前售价 对象实体,注意:跨包访问时可能会是json字符串
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity edit(Object currentPrice) {
		MsgEmity me = ModuleUtil.objToEntity(currentPrice, CurrentPrice.class);//转换数据,因为如果传递的是String,则可能是进行了跨包访问,要先用Json转换成实体对象
		if (!me.isSuccess()) {
			return me;
		}
		
		CurrentPrice currentPriceData = me.getData();
		
		if (null == currentPriceData.getsGoodsId() || "".equals(currentPriceData.getsGoodsId())) {
			return MsgEmity.err(8001, "物品编号不能为空");
		}
		
		if (null == currentPriceData.getmPrice() || (currentPriceData.getmPrice().compareTo(BigDecimal.ZERO) == -1)) {
			return MsgEmity.err(8002, "售价不能小于0");
		}
		
		if (null == currentPriceData.getmMinPrice() || (currentPriceData.getmMinPrice().compareTo(BigDecimal.ZERO) == -1)) {
			return MsgEmity.err(8003, "最低价格不能小于0");
		}
		
		if (null == currentPriceData.getmMaxPrice() || (currentPriceData.getmMaxPrice().compareTo(BigDecimal.ZERO) == -1)) {
			return MsgEmity.err(8004, "最高价格不能小于0");
		}
		
		if (currentPriceData.getmPrice().compareTo(currentPriceData.getmMinPrice()) == -1) {
			return MsgEmity.err(8005, "售价不能小于最低价格");
		}
		
		if (currentPriceData.getmPrice().compareTo(currentPriceData.getmMaxPrice()) == 1) {
			return MsgEmity.err(8006, "售价不能大于最高价格");
		}
		
		if (currentPriceData.getmMinPrice().compareTo(currentPriceData.getmMaxPrice()) == 1) {
			return MsgEmity.err(8007, "最低价格不能大于最高价格");
		}
		
		if (null == currentPriceData.getiDiscount() || 1 > currentPriceData.getiDiscount() || 10 < currentPriceData.getiDiscount()) {
			return MsgEmity.err(8008, "折扣限制在1到10之间");
		}
		
		if (null == currentPriceData.getiMinDiscount() || 1 > currentPriceData.getiMinDiscount() || 10 < currentPriceData.getiMinDiscount()) {
			return MsgEmity.err(8009, "最低折扣限制在1到10之间");
		}
		
		if (null == currentPriceData.getiMaxDiscount() || 1 > currentPriceData.getiMaxDiscount() || 10 < currentPriceData.getiMaxDiscount()) {
			return MsgEmity.err(8010, "最高折扣限制在1到10之间");
		}
		
		if (currentPriceData.getiDiscount() < currentPriceData.getiMinDiscount()) {
			return MsgEmity.err(8011, "折扣不能小于最低折扣");
		}
		
		if (currentPriceData.getiDiscount() > currentPriceData.getiMaxDiscount()) {
			return MsgEmity.err(8012, "折扣不能大于最高折扣");
		}
		
		if (currentPriceData.getiMinDiscount() > currentPriceData.getiMaxDiscount()) {
			return MsgEmity.err(8013, "最低折扣不能大于最高折扣");
		}

		currentPriceData.setsCreator("00000000");
		currentPriceData.setsModifieder(ModuleUtil.currentLoginUserId());
		
		//--提交数据保存--//
		return currentPriceDao.add(currentPriceData);//注意:更新成功后对象属性iVersion值将被+1
	}

	/**
	 * 根据记录编号取对象
	 * @param sId
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findById(String sId) {
		if (null == sId || "".equals(sId.trim())) {
			return new MsgEmity(false, "编号不能为空！", 8001);
		}
		
		return currentPriceDao.findById(sId);
	}
	
	/**
	 * 根据字段名取指定记录编号的数据库表中对应字段的值
	 * @param sId 编号
	 * @param fieldNames 待取数据的字段名称集合
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity getValueByFieldName(String sId, List<String> fieldNames) {
		if (null == sId || "".equals(sId.trim())) {
			return new MsgEmity(false, "编号不能为空！", 8001);
		}
		
		if (null == fieldNames || fieldNames.size() < 1) {
			return new MsgEmity(false, "待查询字段不能为空！", 8002);
		}
		
		return currentPriceDao.getValueByFieldName(sId, fieldNames);
	}
	
	/**
	 * 根据关键值取对象集合
	 * @param currentPrice 存放查询条件的CurrentPrice(当前售价)对象实体,注意:跨包访问时可能会是json字符串
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findByKey(Object currentPrice) {
		if (null == currentPrice) {
			return new MsgEmity(false, "条件不能为空！", 8000);
		}
		
		MsgEmity me = ModuleUtil.objToEntity(currentPrice, CurrentPrice.class);//转换数据,因为如果传递的是String,则可能是进行了跨包访问,要先用Json转换成实体对象
		if (!me.isSuccess()) {
			return me;
		}
		
		CurrentPrice currentPriceData = me.getData();
		
		return currentPriceDao.findByKey(currentPriceData);
	}

	/**
	 * 根据关键值查数量
	 * @Param currentPrice 存放查询条件的CurrentPrice对象,注意:跨包访问时可能会是json字符串
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findCountByKey(Object currentPrice) {
		if (null == currentPrice) {
			return new MsgEmity(false, "条件对象不能为空！", 8000);
		}
		
		MsgEmity me = ModuleUtil.objToEntity(currentPrice, CurrentPrice.class);//转换数据,因为如果传递的是String,则可能是进行了跨包访问,要先用Json转换成实体对象
		if (!me.isSuccess()) {
			return me;
		}
		
		CurrentPrice currentPriceData = me.getData();
		
		return currentPriceDao.findCountByKey(currentPriceData);
	}
	
	/**
	 * 查询分页
	 * @param page 分页信息对象
	 * @param orders 排序数据集合
	 * @param sLikeStr 全文检索
	 * @param condition 查询条件对象
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findByPage(Page page, OrderInfoList orders, String sLikeStr,
			ConditionCurrentPrice condition) {
		if (null == page) {
			page = new Page(new ArrayList<CurrentPrice>());
		}

		return currentPriceDao.findByPage(page, orders, sLikeStr, condition);
	}

	/**
	 * 根据记录编号查询符合分页数据的某条记录
	 * @param sId
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findByPageRow(String sId) {
		if (null == sId || "".equals(sId.trim())) {
			return new MsgEmity(false, "编号不能为空！", 8001);
		}
		
		return currentPriceDao.findByPageRow(sId);
	}

	/**
	 * 直接调用数据库操作
	 * @param args 参数集合,注意:参数不要使用基本类型
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity callMapping(Object...args) {
		String funName = ModuleUtil.parentMethodName();//待调用的mybits函数名与controller的接口名必须一致
		if (null == funName || "".equals(funName.trim())) {
			return new MsgEmity(false, "调用方法不明！", 8001);
		}
		
		if (null == args) {
			args = new Object[0];
		}
		
		Class<?>[] argsType = new Class<?>[args.length];
		for (int i = 0; i < args.length; i++) {
			argsType[i] = args[i].getClass();
		}
		
		return currentPriceDao.callMapping(funName, argsType, args);
	}

	/**
	 * 创建Excel文件内容
	 * @param orders 排序数据集合
	 * @param sLikeStr 全文检索
	 * @param condition 查询条件对象
	 * @return MsgEmity 返回结构中data为创建的经过压缩的Excel内容,接收端需要进行解压,msg为文件名
	 */
	@Override
	public MsgEmity export(OrderInfoList orders, String sLikeStr, ConditionCurrentPrice condition) {
		MsgEmity me = currentPriceDao.findByExport(orders, sLikeStr, condition);//查询数据
		if(!me.isSuccess()) {
			return me;
		}
		
		me = ModuleUtil.crateExcel(me.getData(), "当前售价.xls");//根据Excel的xml模板创建文件内容
		if(!me.isSuccess()) {
			return me;
		}

		MsgEmity msg = ZipUtil.zipStr(me.getData());//将内容进行压缩,便于传输
		if(!msg.isSuccess()) {
			return msg.setData(100 + (Integer)msg.getData());//提升错误码值
		}
		
		return me.setData(msg.getData());//替换成压缩后的内容
	}
	
	//----------------------------- 自动生成代码结束 -----------------------------//

}
